package com.compile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * Created by zhoulikai on 17/2/17.
 */

public class XmlParser {
    private ParameterClass parameterClass;

    public ParameterClass configureParseFile(String configureFilePath, String fileType) {

        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();

            SAXParser parser = factory.newSAXParser();

            parser.parse(new File(configureFilePath), new MyHandler());
        } catch (Exception e) {

        }

        return parameterClass;
    }

    class MyHandler extends DefaultHandler {
        private ParameterClass.RootBean root;
        private List<ParameterClass.RootBean.ClassBean> classX = new ArrayList<>();
        private ParameterClass.RootBean.ClassBean classBean;
        private List<ParameterClass.RootBean.ClassBean.KeyBean> keyBeanList;
        private ParameterClass.RootBean.ClassBean.KeyBean keyBean;

        @Override
        public void startDocument() throws SAXException {
            System.out.println("start document -> parse begin");
            parameterClass = new ParameterClass();
        }

        @Override
        public void endDocument() throws SAXException {

            System.out.println("end document -> parse finished");
        }

        @Override
        public void startElement(String uri, String localName, String qName,
                                 Attributes attributes) throws SAXException {
            System.out.println("start element-----------");
            System.out.println("    localName: " + localName);
            System.out.println("    qName: " + qName);
            if ("root".equals(qName)) {
                root = new ParameterClass.RootBean();
                root.setName(attributes.getValue("name"));
            } else if ("class".equals(qName)) {
                classBean = new ParameterClass.RootBean.ClassBean();
                classBean.setName(attributes.getValue("name"));
                classBean.setValue(attributes.getValue("value"));
//                keyBeanList.clear();
                keyBeanList = new ArrayList<>();
            } else if ("key".equals(qName)) {
                keyBean = new ParameterClass.RootBean.ClassBean.KeyBean();
                keyBean.setName(attributes.getValue("name"));
                keyBean.setValue(attributes.getValue("value"));
                keyBean.setContent(attributes.getValue("is_require"));
                System.out.println(">>>>>key>>" + keyBean.getName());
//                if (value != null && "true".equals(value)) {
//                    keyBean.setContent(value);
//                } else {
//                    keyBean.setContent(value);
//                }

            }

        }

        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            System.out.println("characters-----------");
            // System.out.println("    ch: " + Arrays.toString(ch) );
            System.out.println("    ch: " + ch);
            System.out.println("    start: " + start);
            System.out.println("    length: " + length);

        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            System.out.println("end element-----------");

            System.out.println("    localName: " + localName);
            System.out.println("    qName: " + qName);
            if ("root".equals(qName)) {
                root.setClassX(classX);
                parameterClass.setRoot(root);
            } else if ("class".equals(qName)) {
                if (keyBeanList != null && !keyBeanList.isEmpty()) {
                    for (int i = 0; i < keyBeanList.size(); i++) {
                        System.out.println(">>keyBeanList>>" + keyBeanList.get(i).getName() + ">>>类名>>>" + classBean.getName());
                    }

                }

                classBean.setKey(keyBeanList);
                classX.add(classBean);
            } else if ("key".equals(qName)) {
                keyBeanList.add(keyBean);
            }

        }
    }

}
